﻿using J.Model;
using J.ProductGet.Application.Command.ProductLabelCommand;
using MediatR;
using SqlSugar;
using StatusDto;

namespace J.ProductGet.Application.CommandHandler.ProductLabelCommandHandler
{
    public class ProductLabelGetCommandHandler : IRequestHandler<ProductLabelGetCommand, PageDto<ProductLabel>>
    {
        private readonly ISqlSugarClient _sqlSugarClient;
        public ProductLabelGetCommandHandler(ISqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }
        public async Task<PageDto<ProductLabel>> Handle(ProductLabelGetCommand request, CancellationToken cancellationToken)
        {
            var list = _sqlSugarClient.Queryable<ProductLabel>();
            if (!string.IsNullOrEmpty(request.LabelName))
            {
                list = list.Where(s => s.LabelName == request.LabelName);
            }
            int PageTotal = list.Count();
            int PageCount = (int)Math.Ceiling(PageTotal * 1.0 / request.pageSize);

            var query = await list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToListAsync();
            return new PageDto<ProductLabel>
            {
                Data = query,
                pagedCount = PageCount,
                pageIndex = request.pageIndex,
                pageSize = request.pageSize,
                totalCount = PageTotal
            };
        }
    }
}
